using System;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using MSHelpLib.BaseCode;

namespace MSHelpLib.HXS.Decoding
{
	/// <summary>
	/// The class <c>HHKParser</c> implements a parser for HHK contents files.
	/// </summary>
	public sealed class HHKParser
	{		
		/// <summary>
		/// Parses a HHK file and returns an ArrayList with the index tree
		/// </summary>
		/// <param name="hhkFile">string content of the hhk file</param>
        /// <param name="helpFile">HXSFile instance</param>
		/// <returns>Returns an ArrayList with the index tree</returns>
		public static ArrayList ParseHHK(string hhkFile, HXSFile helpFile)
		{
			if (helpFile!=null)
				helpFile.OnStatus("Parsing Index Table from "+helpFile.FileName);			
							
			ArrayList IndexList=new ArrayList();
			StringReader fs=new StringReader(hhkFile);									
			fs.ReadLine();
			fs.ReadLine();
			XmlTextReader xmlReader=new XmlTextReader(fs);				
								
			try
			{
				int CurrentIndent=0;
				string Term="";
				System.Collections.Stack History=new Stack();
				IndexItem2 CurrentItem=null;
				while (xmlReader.Read())
				{	
					try
					{
						if (xmlReader.NodeType==XmlNodeType.Whitespace) continue;
						if ((xmlReader.Name!="Jump") && (xmlReader.Name!="Keyword")) continue;

						if (xmlReader.NodeType==XmlNodeType.EndElement)
						{	
							CurrentIndent--;							
							CurrentItem=(IndexItem2)History.Pop();
							continue;
						}
							
						if (xmlReader.NodeType==XmlNodeType.Element)
						{	
							if (!xmlReader.HasAttributes)
								continue;
	                            								
							try
							{
								if (xmlReader.Name=="Keyword")
								{
									Term=xmlReader.GetAttribute("Term");
									IndexItem2 newItem=new IndexItem2(Term);
									newItem.helpFile=helpFile;
									newItem.Indent=CurrentIndent++;																	
									History.Push(CurrentItem);										

									if (CurrentItem==null)
										IndexList.Add(newItem);
									else
										CurrentItem.Children.Add(newItem);

									CurrentItem=newItem;										
								}
								else if (xmlReader.Name=="Jump")
								{
									string Url=xmlReader.GetAttribute("Url");
									if (Url!="")
									{
										if (helpFile!=null)
											CurrentItem.Url=CurrentItem.helpFile.UrlPrefix+CurrentItem.helpFile.FInfo.Name+"::"+Url;
										else
											CurrentItem.Url=Url;
									}
								}
							}
							catch
							{
							}
						}
					}
					catch(Exception ex)
					{
						Debug.WriteLine(ex.Message);
					}
				}				
			}
			catch(Exception ex)
			{
				Debug.WriteLine(ex.Message);
			}
			finally
			{
				// Always close file
				fs.Close();
				fs=null;
			}

			if (helpFile!=null)
				helpFile.OnStatus("Processings...");
			return IndexList;
		}
	}
}
